﻿layui.config({
    version: true,
    base: '/Plugin/'
}).extend({ //设定模块别名
    md5: 'md5', //如果 mymod.js 是在根目录，也可以不用设定别名
    common: 'common' //相对于上述 base 目录的子目录
}).use(['element', 'form', 'table', 'util', 'laydate', 'layer', 'common'], function () {
    var $ = layui.$,
        element = layui.element,
        util = layui.util,
        laydate = layui.laydate,
        table = layui.table,
        layer = layui.layer,
        common = layui.common,
        jhy = layui.data("jhy");
    var tableTask = null,
        tableTaskDone = null;
    //必须在框架内使用
    common.noneIframe();

    var tableload = {
        tabletask: function () {
            tableTask = table.render({
                elem: '#tableTask',
                url: '/Ashx/own.ashx', //数据接口
                method: 'post',
                where: { functionname: 'jhy_task_query' },
                cols: [[ //表头    
                    { type: "radio" },
                    { field: 'task_content', title: '任务', minWidth: 200 },
                    { field: 'task_datetime', title: '时间' }
                ]]
            });
        }, tabletaskdone: function () {
            tableTaskDone = table.render({
                elem: '#tableTaskDone',
                url: '/Ashx/own.ashx', //数据接口
                method: 'post',
                page: {
                    limit: 20,
                    layout: ['prev', 'page', 'next', 'count', 'limit']
                },
                where: { functionname: 'jhy_task_query_done' },
                cols: [[ //表头   
                    { type: "radio" },
                    { field: 'task_content', title: '任务', minWidth: 200, templet: "<div><s>{{d.task_content}}</s></div>" },
                    { field: 'task_datetime', title: '时间' }
                ]]
            });
        }
    }



    tableload.tabletask();
    tableload.tabletaskdone();

    //新增和查询
    util.fixbar({
        bar1: "&#xe642;",
        bgcolor: "#009688",
        click: function (type) {
            if (type === 'bar1') {//新增
                var div = "<div style='padding:10px;'>\
                    <input type='text' class='layui-input' id='task_datetime' style='margin-bottom:3px;'/>\
                    <input type='text' class='layui-input' id='task_content'/>\
                </div>";
                layer.open({
                    type: 1,
                    title: "添加任务",
                    btn: ["取消", "保存"],
                    content: div,
                    skin: "jhy-layer-css",
                    success: function () {
                        laydate.render({
                            elem: '#task_datetime', //指定元素
                            value: new Date(),
                            type: 'datetime',
                            trigger: 'click'
                        });
                    }, yes: function (index, layero) {
                        layer.close(index);
                    }, btn2: function (index, layero) {
                        var task_datetime = $.trim(layero.find("input#task_datetime").val());
                        var task_content = $.trim(layero.find("input#task_content").val());
                        if (task_content == "") {
                            layer.msg("请输入内容");
                            return false;
                        }
                        $.ajax({
                            type: "post",
                            url: "/Ashx/own.ashx",
                            data: { functionname: "jhy_task_add", task_datetime: task_datetime, task_content: task_content },
                            success: function (msg) {
                                if (msg == "true") {
                                    layer.msg("成功");
                                    tableTask.reload();
                                } else {
                                    layer.msg("失败");
                                    tableTask.reload();
                                }
                                layer.close(index);
                            }
                        });
                    }

                });
            }
        }
    });

    //显示及修改操作
    table.on('rowDouble(tableTask)', function (obj) {
        var data = obj.data,
            div = "<div style='padding:10px;'>\
                    <input type='text' class='layui-input' id='task_datetime' style='margin-bottom:3px;'/>\
                    <input type='text' class='layui-input' id='task_content'/>\
                </div>";

        layer.open({
            type: 1,
            title: "修改删除任务",
            btn: ["取消", "删除", "修改"],
            content: div,
            skin: "jhy-layer-css",
            success: function (layero, index) {
                laydate.render({
                    elem: '#task_datetime', //指定元素
                    value: data.task_datetime,
                    type: 'datetime',
                    trigger: 'click'
                });
                layero.find("input#task_content").val(data.task_content);
            }, yes: function (index, layero) {
                layer.close(index);
            }, btn2: function (index, layero) {
                $.ajax({
                    type: "post",
                    url: "/Ashx/own.ashx",
                    data: { functionname: "jhy_task_delete", task_id: data.task_id },
                    success: function (msg) {
                        if (msg == "true") {
                            layer.msg("成功");
                            tableTask.reload();
                        } else {
                            layer.msg("失败");
                            tableTask.reload();
                        }
                        layer.close(index);
                    }
                });
            }, btn3: function (index, layero) {
                var task_datetime = $.trim(layero.find("input#task_datetime").val());
                var task_content = $.trim(layero.find("input#task_content").val());
                if (task_content == "") {
                    layer.msg("请输入内容");
                    return false;
                }
                $.ajax({
                    type: "post",
                    url: "/Ashx/own.ashx",
                    data: { functionname: "jhy_task_edit", task_id: data.task_id, task_datetime: task_datetime, task_content: task_content },
                    success: function (msg) {
                        if (msg == "true") {
                            layer.msg("成功");
                            obj.update({
                                task_content: task_content,
                                task_datetime: task_datetime
                            });
                        } else {
                            layer.msg("失败");
                            tableTask.reload();
                        }
                        layer.close(index);
                    }
                });
            }

        });

    });
    //修改状态
    table.on('radio(tableTask)', function (obj) {
        var data = obj.data;
        if (obj.checked) {
            $.ajax({
                type: "post",
                url: "/Ashx/own.ashx",
                data: { functionname: "jhy_task_done", task_id: data.task_id },
                success: function (msg) {
                    tableTask.reload();
                    tableTaskDone.reload();
                }
            });
        }
    });
    table.on('radio(tableTaskDone)', function (obj) {
        var data = obj.data;
        if (obj.checked) {
            $.ajax({
                type: "post",
                url: "/Ashx/own.ashx",
                data: { functionname: "jhy_task_done", task_id: data.task_id },
                success: function (msg) {
                    tableTask.reload();
                    tableTaskDone.reload();
                }
            });
        }
    });
});


